<template>
  <div class="container-article">
    <el-card>
      <div slot="header">
        <my-bread>内容管理</my-bread>
      </div>

      <el-form label-width="80px">
        <el-form-item label="状态：">
          <el-radio-group v-model="reqParams.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <!--频道组件-->
          <my-channel v-model="reqParams.channel_id"></my-channel>
        </el-form-item>
        <el-form-item label="日期：">
          <el-date-picker
            v-model="dateArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changeData"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="pirmary" @click="filterArticles()">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card style="margin-top:20px">
      <div slot="header">根据筛选条件共查询到 {{total}} 条结果：</div>
      <el-table :data="articles">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" class="cover-image">
              <div slot="error" class="image-slot">
                <img src="../../assets/error.gif" class="cover-image" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title"></el-table-column>

        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status===1">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="发布时间" prop="pubdate"></el-table-column>

        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              @click="toEdit(scope.row.id)"
              type="primary"
              icon="el-icon-edit"
              circle
              plain
            ></el-button>
            <el-button
              @click="delArticle(scope.row.id)"
              type="danger"
              icon="el-icon-delete"
              circle
              plain
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top:20px"
        background
        layout="prev, pager, next"
        :page-size="reqParams.pre_page"
        :current-page="reqParams.page"
        :total="total"
        @current-change="changePage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "article-page",
  data() {
    return {
      dateArr: [],
      reqParams: {
        status: null,
        // 起始时间
        channel_id: null,
        begin_pubdate: null,
        // 结束时间
        end_pubdate: null,
        page: 1,
        pre_page: 20
      },
      articles: [],
      total: 0
    };
  },
  created() {
    this.getArticles();
  },
  methods: {
    delArticle(id) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async()=>{
        try {
          await this.$http.delete(`/articles/${id}`)
          this.$message({
              type: 'success',
              message: '删除成功!'
          });
          this.getArticles()          
        } catch (error) {
          this.$message({
              type: 'error',
              message: '删除失败!'
          });
        }
      }).catch(()=>{})
    },
    toEdit(id) {
      this.$router.push({ path: "/publish", query: { id } });
    },
    changeData(dataArr) {
      if (dataArr) {
        this.reqParams.begin_pubdate = dataArr[0];
        this.reqParams.end_pubdate = dataArr[1];
      } else {
        this.reqParams.begin_pubdate = null;
        this.reqParams.end_pubdate = null;
      }
    },
    filterArticles() {
      this.reqParams.page = 1;
      this.getArticles();
    },

    async getArticles() {
      const {
        data: { data }
      } = await this.$http.get("/articles", { params: this.reqParams });
      this.articles = data.results;
      this.total = data.total_count;
    },
    changePage(newPage) {
      this.reqParams.page = newPage;
      this.getArticles();
    }
  }
};
</script>

<style lang="less" scoped>
.cover-image {
  width: 180px;
  height: 120px;
}
</style>